//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include "refutil.h"
#include "CAStringBufInfo.h"
#include "CVariableOfAStringBuf.h"

UInt32 CAStringBufInfo::AddRef()
{
    Int32 nRef = m_cRef.Increment();
    return (UInt32)nRef;
}

UInt32 CAStringBufInfo::Release()
{
    Int32 nRef = m_cRef.Decrement();

    if (0 == nRef) {
        delete this;
    }

    return nRef;
}

PInterface  CAStringBufInfo::Probe(REIID riid)
{
    if (riid == EIID_IInterface) {
        return (PInterface)this;
    }
    else if (riid == EIID_IAStringBufInfo) {
        return (IAStringBufInfo *)this;
    }
    else if (riid == EIID_IDataTypeInfo) {
        return (IDataTypeInfo *)this;
    }
    else {
        return NULL;
    }
}

ECode CAStringBufInfo::Aggregate(AggregateType type, PInterface pObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CAStringBufInfo::GetDomain(PInterface *ppObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CAStringBufInfo::GetName(
    /* [out] */ AStringBuf * pName)
{
    if (pName == NULL || !pName->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }

    pName->Copy(g_cDataTypeList[CarDataType_AStringBuf].name);
    return NOERROR;
}

ECode CAStringBufInfo::GetSize(
    /* [out] */ MemorySize * pSize)
{
    return E_INVALID_OPERATION;
}

ECode CAStringBufInfo::GetDataType(
    /* [out] */ CarDataType * pDataType)
{
    if (!pDataType) {
        return E_INVALID_ARGUMENT;
    }

    *pDataType = CarDataType_AStringBuf;
    return NOERROR;
}

ECode CAStringBufInfo::CreateVariable(
    /* [in] */ Int32 capacity,
    /* [out] */ IVariableOfAStringBuf ** ppVariableBox)
{
    if (capacity <= 0 || !ppVariableBox) {
        return E_INVALID_ARGUMENT;
    }

    AStringBuf *pAsb =  AStringBuf::Alloc(capacity);
    if (!pAsb) {
        return E_OUT_OF_MEMORY;
    }

    CVariableOfAStringBuf *pVarBox = new CVariableOfAStringBuf(pAsb, TRUE);
    if (pVarBox == NULL) {
        AStringBuf::Free(pAsb);
        return E_OUT_OF_MEMORY;
    }

    *ppVariableBox = (IVariableOfAStringBuf *)pVarBox;
    pVarBox->AddRef();

    return NOERROR;
}

ECode CAStringBufInfo::CreateVariableBox(
    /* [in] */ PCarQuintet variableDescriptor,
    /* [out] */ IVariableOfAStringBuf ** ppVariableBox)
{
    if (!variableDescriptor || !ppVariableBox) {
        return E_INVALID_ARGUMENT;
    }

    PAStringBuf pAsb = (PAStringBuf)variableDescriptor;
    if (!(pAsb->m_flags & CarQuintetFlag_Type_AChar) || !pAsb->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }

    CVariableOfAStringBuf *pVarBox = new CVariableOfAStringBuf(pAsb, FALSE);
    if (pVarBox == NULL) {
        return E_OUT_OF_MEMORY;
    }

    *ppVariableBox = (IVariableOfAStringBuf *)pVarBox;
    pVarBox->AddRef();

    return NOERROR;
}
